import request from '@/common/utils/request'
import {
	serv
} from '@/common/utils/config'

// 分类商品列表
export function getClassGoodLists(data) {
	return new Promise((resolve, reject) => {
		request(`/order-service/api/goods/queryGoodsListWithStandardByCatalog`, {
			method: `post`,
			data: data
		}).then(res => {
			resolve(res.data);
		})
	})
}
// 分类商品列表
export function getClassPageList(data) {
	return new Promise((resolve, reject) => {
		request(`/classify/getPageList`, {
			method: `get`,
			data: data
		}).then(res => {
			console.log('res',res)
			resolve(res.data);
		})
	})
}

// 分类商品列表
export function getOriginGoodsFindByClassifyId(data) {
	return new Promise((resolve, reject) => {
		request(`/originGoods/findByClassifyId`, {
			method: `get`,
			data: data
		}).then(res => {
			console.log('res',res)
			resolve(res.data);
		})
	})
}

// 分类商品列表
export function getProtocolGoodsDetail(data) {
	return new Promise((resolve, reject) => {
		request(`/protocolGoods/detail`, {
			method: `get`,
			data: data
		}).then(res => {
			console.log('res',res)
			resolve(res.data);
		})
	})
}

// 分类商品列表
export function findByOriginGoodsId(data) {
	return new Promise((resolve, reject) => {
		request(`/protocolGoods/findByOriginGoodsId`, {
			method: `get`,
			data: data
		}).then(res => {
			console.log('res',res)
			resolve(res.data);
		})
	})
}

//立即下单接口
export function orderNow(data) {
	return request(serv.order + '/api/preorder/goOrderWithAmount',{
		method: 'post',
		data: data
	})
}
/**
 * 查询库存和价格
 */
export function getPriceAndAmount(data) {
	return new Promise((resolve, reject) => {
		request(serv.order + `/api/goods/getGoodsPriceAndAmount`, {
			method: `post`,
			data: data
		}).then(res => {
			resolve(res.data);
		})
	})
}
/**
 * 切换地址的时候拼接参数
 * @param {*} objs 商品列表
 * @param {*} nums 数量
 * @param {*} areaId 地区id
 * @param {*} purchaserId 采购人单位id
 * @param {*} isqueryAmount 是否要校验库存
 */
export function constructAreaParams(objs,nums = 0,areaId,purchaserId,isqueryAmount,isqueryPrice = true) {
	let params = {};
	let paramsArr = [];
	let queryParams = {}; //查询参数
	queryParams.goods = [];
	for(let i=0;i<objs.length;i++) {
		let item = objs[i];
		paramsArr = [];
		params = {};
		if(item.goodsSourceType == 0) {
			queryParams.queryType = "1";
		}else {
			queryParams.queryType = "2";
		}
		params.goodsCode = item.goodsCode;
		if(nums == 0) {
			nums = item.num;
		}
		if(nums == 0) {
			nums = 1;
		}
		constructParams(paramsArr,params,queryParams,item,nums);
	}
	queryParams.areaId = areaId;
	queryParams.bizDeptId = purchaserId;
	queryParams.isqueryAmount = isqueryAmount;
	queryParams.isqueryPrice = isqueryPrice;
	return queryParams;
}
function constructParams(paramsArr,params,queryParams,item,nums=0) {
	//数量
	params.remainAmount = nums;
	//商品id
	params.protocolGoodsId = item.goodsId;
	//协议id
	params.protocolId = item.protocolId;
	//供应商网络id
	params.supplierId = item.supplierId;
	//商品来源
	params.goodsSourceType = item.goodsSourceType;
	params.protocolType = item.protocolType;
	paramsArr.push(params);
	queryParams.goods = queryParams.goods.concat(paramsArr);
}
// 查询到货时间
export function getDeliveryInfo(data) {
	return request(serv.order + `/api/goods/getDeliveryInfo`, {
		method: `get`,
		data: data
	})
}

// 查询邮费
export function getConfFreight(data) {
	return request(serv.config + `/api/distributionConf/getConfFreight`, {
		method: `get`,
		data: data
	})
}
// 查询库存
export function getGoodsPriceAndAmount(data) {
	return new Promise((resolve, reject) => {
		request(`/order-service/api/goods/getGoodsPriceAndAmount`, {
			method: `post`,
			data: data
		}).then(res => {
			resolve(res.data);
		})
	})
}
// 商品详情
export function getGoodsDetailInfo(data) {
	return new Promise((resolve, reject) => {
		request(`/order-service/api/goods/queryGoodsDetailWithCatalogInfo`, {
			method: `get`,
			data: data
		}).then(res => {
			resolve(res.data);
		})
	})
}
// 获取关注列表
export function favorites(data) {
	return request(`/order-service/api/favorites_product/getCollectionGoodsIds`, {
		method: `get`,
		data: data
	})
}
// 获取评价列表
export function getReviewList(data) {
	return request(`/order-service/api/order/goods/review_page`, {
		method: `get`,
		data: data
	})
}
// 获取好评率
export function getReviewRate(data) {
	return request(`/order-service/api/order/goods/review_count`, {
		method: `get`,
		data: data
	})
}

// 根据productId获取活动
export function getActivitiesByProductId(data) {
	return request(serv.welfare + `/api/activityProduct/getActivitiesByProductId`, {
		method: `get`,
		data: data
	})
}

// 获取关注列表
export function getFavoriteList(data) {
	return request(serv.order + `/api/favorites_product/list`, {
		method: `post`,
		data: data
	})
}

// 收藏商品
export function addCollection(data) {
	return request(serv.order + `/api/favorites_product/addCollection`, {
		method: `post`,
		data: data
	})
}

// 取消收藏商品
export function removeCollection(data) {
	return request(serv.order + `/api/favorites_product/deleteOne`, {
		method: `post`,
		data: data
	})
}

// 浏览历史
export function saveOrUpdateHistory(data) {
	return request(serv.order + `/api/goodsViewHistory/saveOrUpdate`, {
		method: `post`,
		data: data
	})
}

// 新品首发列表
export function getNewList(data) {
	return new Promise((resolve, reject) => {
		request(`/portal-ent/api/shopingMall/queryDefinednH5NewGoods`, {
			method: `post`,
			data: data
		}).then(res => {
			resolve(res.data);
		})
	})
}

/**
 * 营销活动
 */
export function getActivityGoods(id) {
	return request(serv.order + `/api/activity/getActivityGoods?goodsIs=${id}`, {
		method: `get`,
		data: {}
	})
}
